import service from "./axios";
// 方法映射
const methodEnum = {
    get: (url, params = {}) => {
        return new Promise((resolve, reject) => {
            service
                .get(url, { params })
                .then((res) => {
                    resolve(res);
                })
                .catch((error) => {
                    reject(error);
                });
        });
    },
    post: (url, data = {}) => {
        console.log(data);
        return new Promise((resolve, reject) => {
            service
                .post(url, data, {
                    headers: {
                        "Content-Type": "application/json",
                    },
                })
                .then((res) => {
                    resolve(res);
                })
                .catch((error) => {
                    reject(error);
                });
        });
    },
    put: (url, data = {}) => {
        console.log(url, data, "999999999");
        return new Promise((resolve, reject) => {
            service
                .patch(url, data, {
                    headers: {
                        "Content-Type": "application/json",
                    },
                })
                .then((res) => {
                    resolve(res.data);
                }, (err) => {
                    reject(err);
                });
        });
    },
    upload: (url, data = {}) => {
        const { file } = data;
        return new Promise((resolve, reject) => {
            const config = {
                headers: { "Content-Type": "multipart/form-data" },
            };
            let data = new FormData();
            for (const key in file) {
                if (Object.prototype.hasOwnProperty.call(file, key)) {
                    data.append(key, file[key]);
                }
            }
            data.append("file", file);
            return service.post(url, data, config).then((res) => {
                resolve(res.data);
            }, (err) => {
                reject(err);
            });
        });
    },
    delete: (url) => {
        return new Promise((resolve, reject) => {
            service
                .delete(url)
                .then((res) => {
                    resolve(res);
                }, (err) => {
                    reject(err);
                });
        });
    }
};
//  请求方法
const requset = (url, requsetData) => {
    const { method, params } = requsetData;
    return methodEnum[method.toLocaleLowerCase()](url, params)
};
export default requset;